//
//  YKAlertView+Default.m
//  YKAlert
//
//  Created by edward on 2021/9/28.
//

#import "YKAlertView+Default.h"
#import <objc/message.h>


#define screenW UIScreen.mainScreen.bounds.size.width

@implementation YKAlertView (Default)


#pragma mark -提示信息
+ (void)defaultConfigView:(NSString *)title
               message:(NSString *)message
          comformBlock:(void(^ __nullable)(void))comformBlock
           cancelBlock:(void(^ __nullable)(void))cancelBlock;
{
    UIView *configView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW * 0.733, 0)];
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
    }
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [configView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    titleLabel.numberOfLines = 1;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(10, 10, configView.bounds.size.width-20, 22);
    
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.text = message;
    messageLabel.numberOfLines = 0;
    messageLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [configView addSubview:messageLabel];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 5;
    CGSize size = [message boundingRectWithSize:CGSizeMake((configView.bounds.size.width - 20), MAXFLOAT)
                                     options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                  attributes: @{NSFontAttributeName : [UIFont systemFontOfSize:16],
                                                NSParagraphStyleAttributeName : paragraphStyle}
                                     context:nil].size;
    
    messageLabel.frame = CGRectMake(10, CGRectGetMaxY(titleLabel.frame)+10, configView.bounds.size.width - 20, size.height);
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
        titleLabel.textColor = UIColor.labelColor;
        messageLabel.textColor = UIColor.labelColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
        titleLabel.textColor = UIColor.blackColor;
        messageLabel.textColor = UIColor.blackColor;
    }
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    cancelButton.layer.cornerRadius = 8;
    cancelButton.layer.borderColor = UIColor.grayColor.CGColor;
    cancelButton.layer.borderWidth = 1;
    cancelButton.backgroundColor = UIColor.whiteColor;
    [configView addSubview:cancelButton];
    
    [cancelButton addTarget:self action:@selector(dissShowGestureRecognizer:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *configButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [configButton setTitle:@"确认" forState:UIControlStateNormal];
    [configButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    configButton.backgroundColor = UIColor.redColor;
    configButton.layer.cornerRadius = 8;
    configButton.layer.borderColor = UIColor.redColor.CGColor;
    configButton.layer.borderWidth = 1;
    [configView addSubview:configButton];
    configButton.frame = CGRectMake(10, CGRectGetMaxY(messageLabel.frame)+20, (configView.bounds.size.width - 30)/2, 40);
    
    cancelButton.frame = CGRectMake(CGRectGetMaxX(configButton.frame)+10, CGRectGetMaxY(messageLabel.frame)+20, (configView.bounds.size.width - 30)/2, 40);
    objc_setAssociatedObject(configButton, @"myBtn", @{@"block":comformBlock?:^{}}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(cancelButton, @"myBtn", @{@"block":cancelBlock?:^{}}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [configButton addTarget:self action:@selector(dissShowButton:) forControlEvents:UIControlEventTouchUpInside];
    configView.frame = CGRectMake(0, 0, configView.bounds.size.width, CGRectGetMaxY(configButton.frame) + 20);
    [YKAlertView showView:configView showType:YKAlertShowTypeMid bgAutoDissMiss:NO];
}



+ (void)dissShowButton:(UIButton *)sender
{
    void (^callBack)(void) = ((NSDictionary *)objc_getAssociatedObject(sender, @"myBtn"))[@"block"];
    [YKAlertView dissView];
    if (callBack) {
        callBack();
    }
}

#pragma mark -带输入框的弹窗

+ (void)defaultEditView:(nullable NSString *)title
               message:(nullable NSString *)message
        defaultMessage:(nullable NSString *)defaultMessage
       placeHoldString:(nullable NSString *)placeHoldString
          comformBlock:(void(^ __nullable)(NSString * message))comformBlock
           cancelBlock:(void(^ __nullable)(void))cancelBlock
{
    UIView *configView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW * 0.733, 0)];
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
    }
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [configView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    titleLabel.numberOfLines = 1;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(10, 10, configView.bounds.size.width-20, 22);
    
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.text = message;
    messageLabel.numberOfLines = 0;
    messageLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [configView addSubview:messageLabel];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 5;
    CGSize size = [message boundingRectWithSize:CGSizeMake((configView.bounds.size.width - 20), MAXFLOAT)
                                     options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                  attributes: @{NSFontAttributeName : [UIFont systemFontOfSize:16],
                                                NSParagraphStyleAttributeName : paragraphStyle}
                                     context:nil].size;
    
    messageLabel.frame = CGRectMake(10, CGRectGetMaxY(titleLabel.frame)+10, configView.bounds.size.width - 20, size.height);
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
        titleLabel.textColor = UIColor.labelColor;
        messageLabel.textColor = UIColor.labelColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
        titleLabel.textColor = UIColor.blackColor;
        messageLabel.textColor = UIColor.blackColor;
    }
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(messageLabel.frame) + 10, configView.bounds.size.width - 20, 30)];
    textField.layer.borderColor = UIColor.blackColor.CGColor;
    textField.layer.borderWidth = 1;
    textField.text = defaultMessage;
    textField.placeholder = placeHoldString;
    [configView addSubview:textField];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    cancelButton.layer.cornerRadius = 8;
    cancelButton.layer.borderColor = UIColor.grayColor.CGColor;
    cancelButton.layer.borderWidth = 1;
    cancelButton.backgroundColor = UIColor.whiteColor;
    [configView addSubview:cancelButton];
    
    [cancelButton addTarget:self action:@selector(dissShowGestureRecognizer:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *configButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [configButton setTitle:@"确认" forState:UIControlStateNormal];
    [configButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    configButton.backgroundColor = UIColor.redColor;
    configButton.layer.cornerRadius = 8;
    configButton.layer.borderColor = UIColor.redColor.CGColor;
    configButton.layer.borderWidth = 1;
    [configView addSubview:configButton];
    configButton.frame = CGRectMake(10, CGRectGetMaxY(textField.frame)+20, (configView.bounds.size.width - 30)/2, 40);
    
    cancelButton.frame = CGRectMake(CGRectGetMaxX(configButton.frame)+10, CGRectGetMaxY(textField.frame)+20, (configView.bounds.size.width - 30)/2, 40);
    
    
    NSString *(^messageBlock)(void) = ^NSString *{
        return textField.text;
    };
    
    objc_setAssociatedObject(configButton, @"myBtn", @{@"block":comformBlock?:^(NSString *message){},@"messageBlock":messageBlock}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    objc_setAssociatedObject(cancelButton, @"myBtn", @{@"block":cancelBlock?:^{}}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [configButton addTarget:self action:@selector(editViewComfirmButton:) forControlEvents:UIControlEventTouchUpInside];
    configView.frame = CGRectMake(0, 0, configView.bounds.size.width, CGRectGetMaxY(configButton.frame) + 20);
    [YKAlertView showView:configView showType:YKAlertShowTypeMid bgAutoDissMiss:NO];
}



+ (void)editViewComfirmButton:(id)sender
{
    void (^callBack)(NSString *message) = ((NSDictionary *)objc_getAssociatedObject(sender, @"myBtn"))[@"block"];
    NSString *(^messageBlock)(void) = ((NSDictionary *)objc_getAssociatedObject(sender, @"myBtn"))[@"messageBlock"];
    [YKAlertView dissView];
    if (callBack) {
        callBack(messageBlock());
    }
}

#pragma mark -进度条
+ (void)defaultProgressView:(nullable NSString *)title
               message:(nullable NSString *)message
          comformBlock:(void(^ __nonnull)(void(^progress)(float progress)))comformBlock
           cancelBlock:(void(^ __nullable)(void))cancelBlock
{
    UIView *configView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW * 0.733, 0)];
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
    }
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [configView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    titleLabel.numberOfLines = 1;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(10, 10, configView.bounds.size.width-20, 22);
    
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.text = message;
    messageLabel.numberOfLines = 0;
    messageLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [configView addSubview:messageLabel];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 5;
    CGSize size = [message boundingRectWithSize:CGSizeMake((configView.bounds.size.width - 20), MAXFLOAT)
                                     options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                  attributes: @{NSFontAttributeName : [UIFont systemFontOfSize:16],
                                                NSParagraphStyleAttributeName : paragraphStyle}
                                     context:nil].size;
    
    messageLabel.frame = CGRectMake(10, CGRectGetMaxY(titleLabel.frame)+10, configView.bounds.size.width - 20, size.height);
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
        titleLabel.textColor = UIColor.labelColor;
        messageLabel.textColor = UIColor.labelColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
        titleLabel.textColor = UIColor.blackColor;
        messageLabel.textColor = UIColor.blackColor;
    }
    
    UILabel *progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(messageLabel.frame) + 10, configView.bounds.size.width - 20, 20)];
    progressLabel.textColor = UIColor.blueColor;
    progressLabel.text = @"0%";
    progressLabel.font = [UIFont systemFontOfSize:14];
    progressLabel.textAlignment = NSTextAlignmentCenter;
    [configView addSubview:progressLabel];
    
    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    progressView.frame = CGRectMake(10, CGRectGetMaxY(progressLabel.frame) + 10, configView.bounds.size.width - 20, 20);
    progressView.tintColor = [UIColor blueColor];
    progressView.trackTintColor = [UIColor lightGrayColor];
    [configView addSubview:progressView];
    
    
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    cancelButton.layer.cornerRadius = 8;
    cancelButton.layer.borderColor = UIColor.grayColor.CGColor;
    cancelButton.layer.borderWidth = 1;
    cancelButton.backgroundColor = UIColor.whiteColor;
    [configView addSubview:cancelButton];
    
    [cancelButton addTarget:self action:@selector(dissShowGestureRecognizer:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat btnWidth = (configView.bounds.size.width - 30)/2;
    
    cancelButton.frame = CGRectMake((configView.bounds.size.width - btnWidth)/2, CGRectGetMaxY(progressView.frame)+20, btnWidth, 40);

    
    __weak typeof(progressLabel) weaklabel = progressLabel;
    __weak typeof(progressView) weakprogress = progressView;
    void(^progressBlock)(float progress) = ^(float progress){
        weaklabel.text = [NSString stringWithFormat:@"%@%%",@(progress*100).stringValue];
        [weakprogress setProgress:progress animated:YES];
        if (progress >= 1) {
            [YKAlertView dissView];
        }
    };
    
    if (comformBlock) {
        comformBlock(progressBlock);
    }
    
    objc_setAssociatedObject(cancelButton, @"myBtn", @{@"block":cancelBlock?:^{}}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    configView.frame = CGRectMake(0, 0, configView.bounds.size.width, CGRectGetMaxY(cancelButton.frame) + 20);
    [YKAlertView showView:configView showType:YKAlertShowTypeMid bgAutoDissMiss:NO];
}

#pragma mark 头部消息
+ (void)defaultTopTitle:(NSString *)title message:(NSString *)message clickBlock:(void(^ __nullable)(void))clickBlock
{
    UIView *configView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW * 0.733, 0)];
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
    }
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [configView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    titleLabel.numberOfLines = 1;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.frame = CGRectMake(0, 10, configView.bounds.size.width, 22);
    
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.text = message;
    messageLabel.numberOfLines = 0;
    messageLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [configView addSubview:messageLabel];
    messageLabel.textAlignment = NSTextAlignmentLeft;
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 5;
    CGSize size = [message boundingRectWithSize:CGSizeMake((configView.bounds.size.width), MAXFLOAT)
                                     options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                  attributes: @{NSFontAttributeName : [UIFont systemFontOfSize:16],
                                                NSParagraphStyleAttributeName : paragraphStyle}
                                     context:nil].size;
    
    messageLabel.frame = CGRectMake(0, CGRectGetMaxY(titleLabel.frame)+10, configView.bounds.size.width, size.height);
    if (@available(iOS 13.0, *)) {
        configView.backgroundColor = UIColor.systemBackgroundColor;
        titleLabel.textColor = UIColor.labelColor;
        messageLabel.textColor = UIColor.labelColor;
    } else {
        configView.backgroundColor = UIColor.whiteColor;
        titleLabel.textColor = UIColor.blackColor;
        messageLabel.textColor = UIColor.blackColor;
    }
    
    configView.frame = CGRectMake(0, 0, configView.bounds.size.width, CGRectGetMaxY(messageLabel.frame) + 20);
    
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc]init];
    objc_setAssociatedObject(singleTap, @"mySingleTap", @{@"clickBlock":clickBlock?:^{}}, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [singleTap addTarget:self action:@selector(showTopViewClick:)];
    configView.userInteractionEnabled = YES;
    [configView addGestureRecognizer:singleTap];
    
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        [YKAlertView dissView];
    }];
    CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    keyAnim.values = @[@0,@1,@1,@0];
    keyAnim.keyTimes = @[@0,@0.1,@0.9,@1];
    keyAnim.duration = 3;
    keyAnim.repeatCount = 1;
    [configView.layer addAnimation:keyAnim forKey:nil];
    [CATransaction commit];
    
    [YKAlertView showView:configView showType:YKAlertShowTypeFromTop_withNullBac bgAutoDissMiss:YES];
}

+ (void)showTopViewClick:(UITapGestureRecognizer *)sender
{
    void(^clickBlock)(void) = ((NSDictionary *)objc_getAssociatedObject(sender, @"mySingleTap"))[@"clickBlock"];
    [YKAlertView dissView];
    if (clickBlock) {
        clickBlock();
    }
}

#pragma mark 默认取消动作
+ (void)dissShowGestureRecognizer:(id)sender
{
    void (^callBack)(void) = ((NSDictionary *)objc_getAssociatedObject(sender, @"myBtn"))[@"block"];
    [YKAlertView dissView];
    if (callBack) {
        callBack();
    }
}

@end
